package com.seecode;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.NotificationCompat;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;


import com.seecode.WebInterface.JsInterface;

import org.jsoup.select.Evaluator;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/10/20.
 */

public class LightWebView extends WebView  implements View.OnLongClickListener{

    int currentFont=0;


    public LightWebView(Context context) {
        super(context);
        init();
    }

    public LightWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LightWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public LightWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public void init() {

        WebSettings webSettings = getSettings();
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setSupportZoom(true);
        //使用javascript
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);

        setOnLongClickListener(this);

        clearCache(false);
        initActionBar();



    }

    public void show(String[] rawContent, int start, int end, String header, String tail) {
        StringBuilder urlData = new StringBuilder();
        for (int j = start; j <= end; j++) {

            urlData.append(rawContent[j] + "\n");
        }
        addJavascriptInterface(new JsInterface(), "JSInterface");
        loadDataWithBaseURL(null, header + urlData.toString() + tail, "text/html", null, null);
        // Logs.i("comming on we are add gas for you");

    }

    public LightWebView show(String[] rawContent, String header, String tail) {

        show(rawContent, 0, rawContent.length - 1, header, tail);

        return this;

    }

    public void setLook(boolean isSee) {
        if (isSee) {
            setVisibility(VISIBLE);
            Logs.i("I will could see");
        } else {
            setVisibility(INVISIBLE);
            //Logs.i("I will could  not see");
        }
    }

    public void moveToFunName(String funName) {
        String jsString;

        jsString = "javascript:gotoCode('" + funName + "');";

        //Logs.i("jscode "+jsString);
        evaluateJavascript(jsString, null);
    }


    @Override
    public boolean onLongClick(View v) {
        HitTestResult hitTestResult =  getHitTestResult();
      String conent= hitTestResult.getExtra();
        Logs.i("weblogs:content:"+conent);

    String jsString;

            jsString = "javascript:getSelectedText();";
           Logs.i("weblogs before now exe js" + jsString);
            //Logs.i("jscode "+jsString);
            evaluateJavascript(jsString, null);
           // loadUrl(jsString);
            Logs.i("weblogs loadurl now exe js" + jsString);

        //showContextMenu(200,200);
            return true;
    }


    /*protected void onCreateContextMenu(ContextMenu menu) {


        menu.add(Menu.NONE, 1, 0,"查看");
        menu.add(Menu.NONE, 2, 0,"保存到相册");
        menu.add(Menu.NONE, 3, 0,"删除");
        menu.add(Menu.NONE, 4, 0,"取消");
    }*/
    ArrayList<String> mActionList=new ArrayList<>();
    ActionMode mActionMode;
    private ActionMode resolveActionMode(ActionMode actionMode) {
        Logs.i("acton resolveAction");
        if(actionMode != null) {
            final Menu menu = actionMode.getMenu();
            mActionMode = actionMode;

            menu.clear();
            for (int i = 0; i < mActionList.size(); i++) {
                menu.add(mActionList.get(i));
            }
            for (int i = 0; i < menu.size(); i++) {
                MenuItem menuItem = menu.getItem(i);
                menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        //getSelectedData((String) item.getTitle());
                        //releaseAction();
                        return true;
                    }
                });
            }
        }
        mActionMode = actionMode;


        return actionMode;
    }
    @Override
    public ActionMode startActionMode(ActionMode.Callback callback) {
        ActionMode actionMode = super.startActionMode(callback);
        return resolveActionMode(actionMode);

    }

    @Override
    public ActionMode startActionMode(ActionMode.Callback callback, int type) {
        ActionMode actionMode = super.startActionMode(callback, type);
        return resolveActionMode(actionMode);
    }
    public void initActionBar(){


    }

    public void adjustFont(){

        currentFont =currentFont+1;
        if(currentFont > 4){
            currentFont=0;
        }

        WebSettings webset = getSettings();
        switch (currentFont){

            case 0:
                webset.setTextSize(WebSettings.TextSize.SMALLEST);
                break;
            case 1:
                webset.setTextSize(WebSettings.TextSize.SMALLER);
                break;
            case 2:
                webset.setTextSize(WebSettings.TextSize.NORMAL);
                break;
            case 3:
                webset.setTextSize(WebSettings.TextSize.LARGER);
                break;
            case 4:
                webset.setTextSize(WebSettings.TextSize.LARGEST);
                break;
            default:


        }
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        return super.onKeyLongPress(keyCode, event);
    }

}
